/*
 * @Description: 
 * @Author: TzzzT
 * @Date: 2022-03-12 11:19:06
 * @LastEditTime: 2023-04-18 04:17:22
 * @LastEditors: TzzzT
 */
#ifndef _MY_BLE_H
#define _MY_BLE_H


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "esp_bt_defs.h"

/* Attributes State Machine */
enum
{
    IDX_RCS_SVC,
    IDX_CHAR_CLI_TO_SER,
    IDX_CHAR_VAL_CLI_TO_SER,
    IDX_CHAR_SER_TO_CLI,
    IDX_CHAR_VAL_SER_TO_CLI,
    IDX_CHAR_CFG_SER_TO_CLI,
    RCS_SERV_IDX_NB,
};

#define PROFILE_NUM      3
#define PROFILE_GUNPOINT_ID 0
#define PROFILE_GUNSCREEN_ID 1
#define PROFILE_ARMOR_ID 2

#define SAMPLE_DEVICE_NAME          "RCS-MASTER"


/* The max length of characteristic value. When the GATT client performs a write or prepare write operation,
*  the data length must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX. 
*/
#define GATTS_DEMO_CHAR_VAL_LEN_MAX 500
#define PREPARE_BUF_MAX_SIZE        1024
#define CHAR_DECLARATION_SIZE       (sizeof(uint8_t))

#define ADV_CONFIG_FLAG             (1 << 0)
#define SCAN_RSP_CONFIG_FLAG        (1 << 1)

#define RSSI_LOWEST (-80)
// typedef void(*physical_connected_cb_t)(esp_bd_addr_t);
// typedef void(*virtual_connected_cb_t)(uint8_t, esp_bd_addr_t, bool);
typedef void(*scan_rst_cb_t)(esp_bd_addr_t, uint8_t*, uint8_t, int8_t, uint8_t*);
typedef void(*notify_evt_cb_t)(uint8_t, esp_bd_addr_t, uint8_t*, size_t);
typedef void(*disconnect_cb_t)(uint8_t, esp_bd_addr_t, uint8_t);
typedef void(*open_cb_t)(uint8_t, esp_bd_addr_t, bool);
typedef void(*read_rssi_cb_t)(void);


typedef struct client_func_s{
    // physical_connected_cb_t phy_conn_cb;   //
    // virtual_connected_cb_t  vir_conn_cb;
    notify_evt_cb_t  notify_cb;
    disconnect_cb_t  disconn_cb;
    open_cb_t open_cb;
    scan_rst_cb_t scan_result_cb;
    read_rssi_cb_t read_rssi_cb;
}client_func_t;

client_func_t* get_client_func_p(void);

void ble_client_init(void);
void set_remote_bda(uint8_t equip_sn, esp_bd_addr_t remote_bda);
void clean_remote_bda(uint8_t equip_sn);
bool is_physical_connected(uint8_t equip_sn);
bool is_virual_connected(uint8_t equip_sn);
void close_physical_connection(uint8_t equip_sn);
void start_connect(uint8_t equip_sn);
void ble_client_set_scan_param(uint32_t scan_duration);
void stop_scan(void);
esp_err_t write_char(uint8_t equip_sn, uint8_t *package, uint16_t len);

#endif  // _MY_BLE_H